﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExplodeGeometries.CApEXGExplodeGeometriesApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExplodeGeometries;

internal class CApEXGExplodeGeometriesApp
{
  public void ExplodeSelectedObjects()
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    ArrayList failedEntitiesList = new ArrayList();
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    TxObjectList allGeometries = this.GetAllGeometries();
    foreach (ITxApproximation itxApproximation in (Collection<ITxObject>) allGeometries)
    {
      if (itxApproximation != null)
      {
        TxExplodeGeometryState canExplodeState = (TxExplodeGeometryState) 4;
        if (itxApproximation.CanExplodeGeometry(ref canExplodeState))
        {
          string name = ((ITxObject) itxApproximation).Name;
          ITxGroup parent = itxApproximation.ExplodeGeometry();
          ((ITxObject) parent).Name = name;
          this.CheckChildNames((ITxObjectCollection) parent);
        }
        else
        {
          string explodeFailReason = this.GetExplodeFailReason(canExplodeState);
          failedEntitiesList.Add((object) string.Format(explodeFailReason, (object) ((ITxObject) itxApproximation).Name));
        }
      }
    }
    if (((Collection<ITxObject>) allGeometries).Count > 0 && ((Collection<ITxObject>) allGeometries).Count != failedEntitiesList.Count)
    {
      TxApplication.ActiveUndoManager.ClearAllTransactions();
      this.RestoreSelection(allItems);
      TxApplication.RefreshDisplay();
    }
    Cursor.Current = current;
    this.ShowFailMessage(failedEntitiesList);
  }

  private void RestoreSelection(TxObjectList selectedItems)
  {
    TxApplication.ActiveSelection.Clear();
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject selectedItem in (Collection<ITxObject>) selectedItems)
    {
      if (selectedItem != null && selectedItem.IsValid())
        ((Collection<ITxObject>) txObjectList).Add(selectedItem);
    }
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }

  private void ShowFailMessage(ArrayList failedEntitiesList)
  {
    if (failedEntitiesList.Count <= 0)
      return;
    StringBuilder stringBuilder = new StringBuilder();
    foreach (string failedEntities in failedEntitiesList)
    {
      stringBuilder.Append(failedEntities);
      stringBuilder.Append("\n");
    }
    CUiEXGExplodeGeometryMessage.Show(stringBuilder.ToString());
  }

  private string GetExplodeFailReason(TxExplodeGeometryState canExplodeState)
  {
    string explodeFailReason = string.Empty;
    switch (canExplodeState - 1)
    {
      case 0:
        explodeFailReason = ExplodeGeometriesStringTable.CANNOT_EXPLODE_ENTITY_GEOMETRY_IS_LOADED;
        break;
      case 1:
        explodeFailReason = ExplodeGeometriesStringTable.CANNOT_EXPLODE_ENTITY_GEOMETRIES_INVALID_GEOMETRY;
        break;
      case 2:
        explodeFailReason = ExplodeGeometriesStringTable.CANNOT_EXPLODE_ENTITY_NO_GEOMETRY;
        break;
      case 3:
        explodeFailReason = ExplodeGeometriesStringTable.CANNOT_EXPLODE_ENTITY_GEOMETRIES_INVALID_OBJECT;
        break;
    }
    return explodeFailReason;
  }

  private TxObjectList GetAllGeometries()
  {
    TxObjectList allGeometries = new TxObjectList();
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (TxGeometry));
    foreach (ITxObject itxObject in (Collection<ITxObject>) allItems)
    {
      if (itxObject is TxGeometry)
      {
        if (!((Collection<ITxObject>) allGeometries).Contains(itxObject))
          ((Collection<ITxObject>) allGeometries).Add(itxObject);
      }
      else if (itxObject is ITxObjectCollection objectCollection)
      {
        foreach (ITxObject allDescendant in (Collection<ITxObject>) objectCollection.GetAllDescendants((ITxTypeFilter) txTypeFilter))
        {
          if (!((Collection<ITxObject>) allGeometries).Contains(allDescendant))
            ((Collection<ITxObject>) allGeometries).Add(allDescendant);
        }
      }
    }
    return allGeometries;
  }

  private void CheckChildNames(ITxObjectCollection parent)
  {
    if (parent == null)
      return;
    foreach (ITxObject directDescendant in (Collection<ITxObject>) parent.GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxObject))))
    {
      string name = directDescendant.Name;
      if (directDescendant.Name == string.Empty)
      {
        string str;
        switch (directDescendant)
        {
          case TxArc _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_ARC;
            break;
          case TxSolid _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_SOLID;
            break;
          case TxPolyhedron _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_POLYHEDRON;
            break;
          case TxPolyline _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_POLYLINE;
            break;
          case TxCurve _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_CURVE;
            break;
          case TxLine _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_LINE;
            break;
          case TxGeometry _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_GEOMETRY;
            break;
          case TxSurface _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_SURFACE;
            break;
          case TxGroup _:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_GROUP;
            break;
          default:
            str = ExplodeGeometriesStringTable.EG_GEOMETRY_OBJECT;
            break;
        }
        directDescendant.Name = TxObjectEx.GetUniqueDisplayName((ITxObject) parent, str);
      }
    }
  }
}
